#include "tdeklaracio.h"
#include <sstream>
#include <iterator>

using namespace std;

void TDeklaracio::ujatri(std::string nev, std::string ertek){
    bool l=false;
    for(vector<Atributum>::iterator it = atributumok.begin(); it != atributumok.end() && !l; ++it){
        l = it->nev == nev;
    }
    if(!l){
        Atributum seged;
        seged.nev = nev; seged.ertek = ertek;
        atributumok.push_back(seged);
    }else { throw ATRI_EXIST;}
}

void TDeklaracio::modatri(std::string nev, std::string ertek){
    if (atributumok.size() > 0){
        bool l=false;
        int i = 0;
        for(; i < atributumok.size() && !l; ++i){
            l = atributumok[i].nev == nev;
        }
        if (l){
            atributumok[i-1].ertek = ertek;
        }else {throw ATRI_NEXIST;}
    }else {throw ATRI_NEXIST;}
}

void TDeklaracio::torlesatri(std::string nev){
    if (atributumok.size() > 0){
        bool l=false;

        for(vector<Atributum>::iterator it = atributumok.begin();
        it !=  atributumok.end() && !l; ++it){

            if (l = (it->nev == nev)){atributumok.erase(it);}
        }
        if (!l){throw ATRI_NEXIST;}
    }else {throw ATRI_NEXIST;}
}
void TDeklaracio::modosit(string nev){
    formatum = nev;
}

string TDeklaracio::toString(){
    ostringstream os;
    os << "<?" << formatum << " ";
    for (vector<Atributum>::iterator it = atributumok.begin(); it != atributumok.end(); ++it){
        os << it->nev << "=\""<< it->ertek << "\" ";
    }
    os << "?>";

    return os.str();
}

string TDeklaracio::konzolrair(){
    ostringstream os;
    os << "Formatum: " << formatum << endl;
    os << "Atributumok: " << endl;
    for (vector<Atributum>::iterator it = atributumok.begin();it != atributumok.end(); ++it){
        os << it->nev << "=\""<< it->ertek << "\"\t";
    }
    return os.str();
}
